<!-- 报警设置 -->
<!DOCTYPE>
<html>

<head>
    <%- include ../taglib/layout-table-tree.ejs %>

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content" style="padding:5px">
    <div class="row animated fadeInDown">
        <div class="col-sm-3">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>车辆数据</h5>
                </div>
                <div class="ibox-content  pre-scrollable">
                    <div id='external-events'>
                        <ul id="kindTree" class="ztree">
                        </ul>
                    </div>
                </div>
            </div>

        </div>
        <div class="col-sm-9 clearfix" style="padding-left:5px">

            <div class="row">
                <div class="col-sm-12">

                    <div class="ibox float-e-margins ">
                        <div class="ibox-title">
                            <h5>数据列表</h5>

                            <div class="ibox-tools">
                            </div>
                        </div>
                        <div class="ibox-content">

                            <div class="" id="">
                                <button class="btn btn-primary " type="button" onclick="add()"><i
                                            class="fa fa-check"></i>&nbsp;新增
                                </button>
                                <button class="btn btn-danger " type="button" onclick="dels()"><i
                                            class="fa fa-close"></i>
                                    批量删除
                                </button>
                                <div>
                                    <table id="example"
                                           class="table table-striped table-bordered table-hover dataTables-example">
                                        <thead>
                                        <tr>
                                            <th>
                                            </th>
                                            <th width="200px">id</th>
                                            <th width="200px">carid</th>
                                            <th width="200px">车牌号</th>
                                            <th width="200px">超速报警</th>
                                            <th width="200px">限速值</th>
                                            <th width="200px">停车报警</th>
                                            <th width="200px">限时值</th>
                                            <th width="200px">凌晨报警</th>
                                            <th width="200px">疲劳驾驶报警</th>
                                            <th width="200px">连续驾驶限时值/秒</th>
                                            <th width="200px">最小休息时间/秒</th>
                                            <th width="200px" class="show-detail-json">操作</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        </tbody>
                                    </table>
                                </div>
                                <div class="modal inmodal" id="myModal" role="dialog" data-width="500px"
                                     data-height="400px" aria-hidden="true">
                                    <div class="modal-dialog">
                                        <div class="modal-content animated bounceInRight">
                                            <div class="modal-header">
                                                <h4 class="modal-title">新增报警设置</h4>
                                            </div>
                                            <div class="modal-body" style="max-height: 400px; ">

                                                <form class="form-horizontal m-t" id="editForm" method="get">
                                                    <input id="id" name="id" type="hidden">
                                                    <input id="carid" name="carid" type="hidden">
                                                    <input type="hidden" id="h_c_car_id">
                                                    <input type="hidden" id="h_all_car_id" name="carIds">
                                                    <!--<input id="deptId" name="deptId" type="hidden">-->

                                                    <div class="form-group">
                                                        <label class="col-sm-3 control-label">超速报警</label>
                                                        <div class="col-sm-8">
                                                            <select class="form-control m-b" id="isSpeed"
                                                                    name="isSpeed" type="text">
                                                                <option value="0">关闭</option>
                                                                <option value="1">开启</option>
                                                            </select>
                                                        </div>
                                                    </div>
                                                    <div class="form-group">
                                                        <label class="col-sm-3 control-label">限速值</label>
                                                        <div class="col-sm-8">
                                                            <input id="speedLimited" name="speedLimited"
                                                                   class="form-control"
                                                                   placeholder="限速值必填" type="number"
                                                                   aria-required="true"
                                                                   aria-invalid="true">
                                                            <!--可以增加提示-->
                                                            <!--<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 这里写点提示的内容</span>-->
                                                        </div>
                                                    </div>
                                                    <div class="form-group">
                                                        <label class="col-sm-3 control-label">停车报警</label>
                                                        <div class="col-sm-8">
                                                            <select class="form-control m-b" id="isStop"
                                                                    name="isStop" type="text">
                                                                <option value="0">关闭</option>
                                                                <option value="1">开启</option>
                                                            </select>
                                                        </div>
                                                    </div>
                                                    <div class="form-group">
                                                        <label class="col-sm-3 control-label">限时值</label>
                                                        <div class="col-sm-8">
                                                                <input id="stopLimited" name="stopLimited"
                                                                       class="form-control"
                                                                       placeholder="限时值必填" type="number"
                                                                       aria-required="true"
                                                                       aria-invalid="true">
                                                        </div>
                                                    </div>
                                                    <div class="form-group">
                                                        <label class="col-sm-3 control-label">凌晨报警</label>
                                                        <div class="col-sm-8">
                                                                <select class="form-control m-b" id="isWeeHours"
                                                                        name="isWeeHours" type="text">
                                                                    <option value="0">关闭</option>
                                                                    <option value="1">开启</option>
                                                                </select>
                                                        </div>
                                                    </div>
                                                    <div class="form-group">
                                                        <label class="col-sm-3 control-label">疲劳驾驶报警</label>
                                                        <div class="col-sm-8">
                                                                    <select class="form-control m-b" id="isFatigue"
                                                                            name="isFatigue" type="text">
                                                                        <option value="0">关闭</option>
                                                                        <option value="1">开启</option>
                                                                    </select>
                                                        </div>
                                                    </div>
                                                    <div class="form-group">
                                                        <label class="col-sm-3 control-label">连续驾驶限时值/秒</label>
                                                        <div class="col-sm-8">
                                                            <input id="driverLimitedTime" name="driverLimitedTime"
                                                                   class="form-control" placeholder="连续驾驶限时值/秒必填"
                                                                   type="number"
                                                                   aria-required="true" aria-invalid="true">
                                                            <!--可以增加提示-->
                                                            <!--<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 这里写点提示的内容</span>-->
                                                        </div>
                                                    </div>
                                                    <div class="form-group">
                                                        <label class="col-sm-3 control-label">最小休息时间/秒</label>
                                                        <div class="col-sm-8">
                                                            <input id="restLimitedTime" name="restLimitedTime"
                                                                   class="form-control" placeholder="最小休息时间/秒必填"
                                                                   type="number"
                                                                   aria-required="true" aria-invalid="true">
                                                            <!--可以增加提示-->
                                                            <!--<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 这里写点提示的内容</span>-->
                                                        </div>
                                                    </div>


                                                </form>
                                            </div>
                                            <div class="modal-footer">

                                                <button type="button" class="btn btn-white" onclick="closeModal()">关闭
                                                </button>
                                                <button type="button" class="btn btn-primary" onclick="save()">保存
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                            </div>
                        </div>
                    </div>


                </div>

            </div>
        </div>
    </div>
</div>

<%- include ../taglib/layout-content.ejs %>

<script src="js/combox/comb-carType-data.js"></script>
<script src="js/combox/comb-terminal-data.js"></script>

<script type="text/javascript">
    var table;
    var validator;
    $(document).ready(function () {
        table = $('.dataTables-example').dataTable({
            "lengthChange": false,//是否允许用户自定义显示数量
//            "searching": false,//本地搜索
            "scrollY": true,//dt高度
            scrollX: true,
            "autoWidth": true,//自动宽度
            "dom": '<"top"<"clear">>rt<"bottom"ilp<"clear">>',
//            "lengthChange": false,//是否允许用户自定义显示数量
            "ajax": {
                "url": "/baseCarCommonSet/list?carId=0",
                "dataSrc": "rows"
            },
            serverSide: true,
            columns: [
                {
                    "sClass": "text-center",
                    "data": "id",
                    'checkboxes': {
                        'selectRow': true,
                    }
                },
                {"sClass": "text-center", data: 'id'},
                {"sClass": "text-center", data: 'carid', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'carNo', "bSortable": false, "orderable": false},
                {
                    "sClass": "text-center", data: function (e) {//这里给最后一列返回一个操作列表
                    //e是得到的json数组中的一个item ，可以用于控制标签的属性。
                    var d = e.isSpeed;
//                    1-驶入  2-驶出 3-超速 4-路线偏离 5-停车超时报警  6-疲劳驾驶报警 7-凌晨行车报警
                    var result = "";
                    if (d == 0) {
                        result = '关闭'
                    } else if (d == 1) {
                        result = '开启'
                    }
                    return result;
//                    return '<a class="btn btn-danger btn-rounded" >'+result+'</a>'
                }, "bSortable": false, "orderable": false
                },
                {"sClass": "text-center", data: 'speedLimited', "bSortable": false, "orderable": false},
//                {"sClass": "text-center", data: 'isStop', "bSortable": false, "orderable": false},
                {
                    "sClass": "text-center", data: function (e) {//这里给最后一列返回一个操作列表
                    //e是得到的json数组中的一个item ，可以用于控制标签的属性。
                    var d = e.isStop;
                    debugger;

//                    1-驶入  2-驶出 3-超速 4-路线偏离 5-停车超时报警  6-疲劳驾驶报警 7-凌晨行车报警
                    var result = "";
                    if (d == 0) {
                        result = '关闭'
                    } else if (d == 1) {
                        result = '开启'
                    }
                    return result;
//                    return '<a class="btn btn-danger btn-rounded" >'+result+'</a>'
                }, "bSortable": false, "orderable": false
                },
                {"sClass": "text-center", data: 'stopLimited', "bSortable": false, "orderable": false},
                {
                    "sClass": "text-center", data: function (e) {//这里给最后一列返回一个操作列表
                    //e是得到的json数组中的一个item ，可以用于控制标签的属性。
                    var d = e.isWeeHours;
//                    1-驶入  2-驶出 3-超速 4-路线偏离 5-停车超时报警  6-疲劳驾驶报警 7-凌晨行车报警
                    var result = "";
                    if (d == 0) {
                        result = '关闭'
                    } else if (d == 1) {
                        result = '开启'
                    }
                    return result;
//                    return '<a class="btn btn-danger btn-rounded" >'+result+'</a>'
                }, "bSortable": false, "orderable": false
                },
                {
                    "sClass": "text-center", data: function (e) {//这里给最后一列返回一个操作列表
                    //e是得到的json数组中的一个item ，可以用于控制标签的属性。
                    var d = e.isFatigue;
                    var result = "";
                    if (d == 0) {
                        result = '关闭'
                    } else if (d == 1) {
                        result = '开启'
                    }
                    return result;
//                    return '<a class="btn btn-danger btn-rounded" >'+result+'</a>'
                }, "bSortable": false, "orderable": false
                },
                {"sClass": "text-center", data: 'driverLimitedTime', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'restLimitedTime', "bSortable": false, "orderable": false},


                {
                    "bSortable": false,//禁止排序
                    "orderable": false,
                    "sClass": "text-center",
                    data: function (d, e, f, g) {//这里给最后一列返回一个操作列表
                        //e是得到的json数组中的一个item ，可以用于控制标签的属性。
                        var id = d.id
                        var index = g.row
                        return '' +
                            '<button class="btn btn-info btn-sm btn-select-row" type="button" onclick="edit(' + index + ')" data-toggle="modal"   ><i class="fa fa-edit"></i> <span class="bold">编辑</span>' +
                            '<button class="btn btn-danger btn-sm btn-select-row" type="button"  onclick="del(' + id + ')" ><i class="fa fa-close"></i> <span class="bold">删除</span>';

                    }
                }
            ],
            //排序
            'order': [[1, 'desc']],
            initComplete: function (setting, json) {
                //隐藏列
                visiblecolumn(1, false);
                visiblecolumn(2, false);
            },
        });

        //验证
        var cusrules = {
            rules: {
                isSpeed: {
                    required: true
                }, speedLimited: {
                    required: true

                }, isStop: {
                    required: true
                }, stopLimited: {
                    required: true
                }, isWeeHours: {
                    required: true
                }, isFatigue: {
                    required: true
                }, driverLimitedTime: {
                    required: true
                }, restLimitedTime: {
                    required: true
                }
            },
            messages: {
                isSpeed: {
                    required: icon + "请设置超速报警",
                    //minlength: icon + "必须3个字符以上",
                    //maxlength: icon + "必须20个字符以下"
                }, speedLimited: {
                    required: icon + "请设置超速报警限值",
                }, isStop: {
                    required: icon + "请设置停车报警",
                }, stopLimited: {
                    required: icon + "请输入停车报警限值",
                    //minlength: icon + "车辆品牌id必须3个字符以上",
                    //maxlength: icon + "车辆品牌id必须20个字符以下"
                }, isWeeHours: {
                    required: icon + "请设置凌晨报警",
                    //minlength: icon + "车辆类型id－面包车、轿车等必须3个字符以上",
                    //maxlength: icon + "车辆类型id－面包车、轿车等必须20个字符以下"
                }, isFatigue: {
                    required: icon + "请设置疲劳驾驶报警",

                }, driverLimitedTime: {
                    required: icon + "请输入连续驾驶限时值",
                    //minlength: icon + "车辆图标必须3个字符以上",
                    //maxlength: icon + "车辆图标必须20个字符以下"
                }, restLimitedTime: {
                    required: icon + "请输入最小休息时间",
                }
            }

        };
        validator = $("#editForm").validate(cusrules);
//        loadComb("baseTerminalIdsearch", combTerminal);

//        下拉列表
        $("#myModal").on("shown.bs.modal", function () {
//            loadComb("baseTerminalId", combTerminal);
//            loadComb("baseCarTypeId", combCarType);

        })

    });

    function add() {
        //选择tree
        var carIds=$("#h_all_car_id").val();
        if(carIds){
            $.ajax({
                type: "GET",
                url: "/baseCarCommonSet/create",
                cache: false,
                async: false,
                dataType: "json",
                success: function (data) {
                    $('#editForm').loadDate(data);
                    $("#myModal").modal("show");
                }
            });
        }else{
            swal("选择错误", "请勾选车辆添加报警设置!", "warning");
            return false;
        }




    }
    function edit(index) {
        var data = table.DataTable().rows(index).data()
        if (data && data[0]) {
            $('#editForm').loadDate(data[0]);
            $("#myModal").modal("show");
        } else {
            swal("已取消", "数据错误！", "warning");
        }
    }
    function del(id) {
        swal({
                title: "您确定要删除这条信息吗",
                text: "删除后将无法恢复，请谨慎操作！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的，我要删除！",
                cancelButtonText: "让我再考虑一下…",
                closeOnConfirm: false,
                closeOnCancel: false
            },
            function (isConfirm) {
                if (isConfirm) {
                    $.ajax({
                        type: "GET",
                        url: "/baseCarCommonSet/del",
                        cache: false,
                        async: false,
                        data: {
                            ids: id
                        },
                        dataType: "json",
                        success: function (data) {
//                            swal("删除成功！", "您已经永久删除了这条信息。", "success");
                            swal("删除成功！", data, "success");
                            query(0);
                        }
                    });

                } else {
                    swal("已取消", "您取消了删除操作！", "error");
                }
            });
    }
    function dels() {

        var rows = table.DataTable().rows('.selected').data();
        if (rows && rows.length >= 1) {

            var ids = "";
            rows.each(function (value) {
                ids += value.id + ",";
            })
            debugger;
            del(ids);
        } else {
            swal("系统提示", "请选择需要删除的数据！", "warning");
        }

    }
    function query(carId) {
        var code;
        var jsonstr;
        var selectedNode = tree_Obj.getSelectedNodes()[0];
        var carId;
        var carIds=$("#h_all_car_id").val();
        if (carIds) {
            carId = carIds;
        } else {
            carId = selectedNode.id+",";

        }
        jsonstr = [{"carid": carId}];
        table.DataTable().search(JSON.stringify(jsonstr), false, false).draw();
    }
    function save() {
        var selectedNode = tree_Obj.getSelectedNodes()[0];
        $("#carid").val(selectedNode.id);
        $('#editForm').ajaxSubmit({
            type: 'post', // 提交方式 get/post
            dataType: 'json',
            url: '/baseCarCommonSet/save', // 需要提交的 url
            beforeSubmit: function (formData, jqForm, options) {
                if (!$("#editForm").valid()) {
                    return false;
                }
            },
            success: function (data) {
                // data 保存提交后返回的数据，一般为 json 数据
                // 此处可对 data 作相关处理
                if (data.success) {
                    swal({
                        title: "系统提示",
                        text: data.msg,
                        type: "success"
                    });
                    $("#myModal").setDefForm(validator);
                    $('#editForm').clearForm(true);
                    $("#myModal").modal("hide");
//                    loadMyTree(urlTree);
                    query(0);
                } else {
                    swal({
                        title: "系统提示",
                        text: data.msg,
                        type: "error"
                    });
                }
            },

        });
    }
    function closeModal() {
        $("#myModal").setDefForm(validator);
        $("#myModal").modal("hide");
    }
    //隐藏和显示列 c:列0开始，visible=false隐藏
    function visiblecolumn(c, visible) {
        debugger;
        table.DataTable().column(c).visible(visible)
    }
    function loadComb(id, data) {
        $("#" + id).select2({
            data: data,
            theme: "bootstrap",
            placeholder: '请选择',
            allowClear: true
        });
    }

    //    部门车辆树
    $(function () {
        urlTree = '/tree/deptCar';
        init_tree('id', 'parentId', 'name', fnClick, fnCheck, "checkbox");
        loadMyTree(urlTree);

    });

    //    树节点选中
    var fnClick = function (event, treeId, treeNode) {
        var flag=treeNode.checked;
//        tree_Obj.checkAllNodes(false);
        tree_Obj.checkNode(treeNode, true, true);
//        cleanAll();
        selectTreeNode(treeNode);


    }
    var fnCheck = function (event, treeId, treeNode, clickFlag) {
        var ids=selectTreeNode(treeNode);

    }

    function selectTreeNode(treeNode) {
        var isCar = treeNode.isCar
        var tChecked = treeNode.checked;
        var ids = setAllCarIds();
        if (isCar) {
            var baseCarNo=treeNode.name;
            if(tChecked){
                $("#h_c_car_id").val(treeNode.id);
            }else{
                $("#h_c_car_id").val("");
            }

        }
        query();
        return ids;
    }
    function setAllCarIds() {
        var checkNodes = tree_Obj.getCheckedNodes(true);
        var ids = "";
        $.each(checkNodes, function (key, value) {
            var flag = value.isCar
            if (flag) {
                ids = ids + value.id + ","
                carno=value.name;
            }
        });
        $("#h_all_car_id").val(ids);
        return ids;
    }
</script>


</body>

</html>
